package com.luban;

import com.luban.entity.User;
import com.luban.service.*;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.SimpleTypeConverter;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.convert.support.DefaultConversionService;

import java.lang.reflect.Method;
import java.util.Locale;
import java.util.function.Supplier;

public class Test {

	public static void main(String[] args) {

		//Thread.currentThread().setContextClassLoader();

		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);

		UserService userService = applicationContext.getBean(UserService.class);
		//System.out.println(aService);
		userService.test();

		//applicationContext.register(AppConfig.class);
		//
		////applicationContext.addBeanFactoryPostProcessor(new LubanXxxBeanFactoryPostProcessor());
		//
		//applicationContext.getBeanFactory().registerSingleton(AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR, new LubanBeanNameGenerator());
		//
		//applicationContext.refresh();

		//applicationContext.register(AppConfig.class);
		//
		//applicationContext.getEnvironment().setRequiredProperties("luban");

		//修改Spring的默认类加载器
		//applicationContext.getBeanFactory().setBeanClassLoader();


		//UserService userService = applicationContext.getBean("userService", UserService.class);
		//userService.test();

		//applicationContext.registerBean(UserService.class, new Supplier<UserService>() {
		//	@Override
		//	public UserService get() {
		//		return new UserService();
		//	}
		//});
		//
		//UserService userService = applicationContext.getBean(UserService.class);
		//
		//System.out.println(userService);

		//User user = applicationContext.getBean(User.class);
		//System.out.println(user);

		//applicationContext.close();

		//applicationContext.registerShutdownHook();
		//System.out.println(applicationContext.getBean("lubanFactoryBean"));
		//System.out.println(applicationContext.getBean("lubanFactoryBean"));
		//System.out.println(applicationContext.getBean("lubanFactoryBean"));


		//ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
		//UserService userService = applicationContext.getBean("userService", UserService.class);
		//userService.test();
		//System.out.println(applicationContext.getBean("userService"));
		//System.out.println(applicationContext.getBean("userService"));

		//SimpleTypeConverter typeConverter = new SimpleTypeConverter();
		//typeConverter.registerCustomEditor(User.class, new StringToUserPropertyEditor());
		//typeConverter.setConversionService(conversionService);
		//User user = typeConverter.convertIfNecessary("1", User.class);
		//System.out.println(user);

		//Spring中的类型转换器
		//DefaultConversionService conversionService = new DefaultConversionService();
		//conversionService.addConverter(new StringToUserConverter());
		//User value = conversionService.convert("1", User.class);
		//System.out.println(value);

		//java中的类型转换器
		//StringToUserPropertyEditor propertyEditor = new StringToUserPropertyEditor();
		//propertyEditor.setAsText("1");
		//User user = (User) propertyEditor.getValue();
		//System.out.println(user);

		//applicationContext.publishEvent("xxx");

		//String message = applicationContext.getMessage("test", null, new Locale("en_CN"));
		//System.out.println(message);


		//ProxyFactory proxyFactory = new ProxyFactory();
		//
		////代理的目标对象
		//proxyFactory.setTarget(new UserService());
		//
		////使用jdk的动态代理还是cglib的动态代理
		////proxyFactory.setTargetClass(UserService.class);
		//
		////使用jdk的动态代理
		//proxyFactory.addInterface(UserInterface.class);
		//
		////使用cglib
		//proxyFactory.setProxyTargetClass(true);
		//
		//
		//proxyFactory.addAdvice(new MethodBeforeAdvice() {
		//	@Override
		//	public void before(Method method, Object[] args, Object target) throws Throwable {
		//		System.out.println("执行方法前");
		//		method.invoke(target, args);
		//	}
		//});
		//
		//UserInterface proxy = (UserInterface) proxyFactory.getProxy();
		//
		////before  ----> target.test()
		//proxy.test();
	}
}
